﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.IO;
using System.Text;
using Xpom_Xpum__Integrated_Development_Environment.Model;

namespace Xpom_Xpum__Integrated_Development_Environment.ViewModel
{
    public class IntegratorSettingsWindowViewModel : ViewModel
    {
        #region Constructor

        public IntegratorSettingsWindowViewModel()
        {
            Initialize();
        }

        #endregion

        #region Commands

        #endregion

        #region Fields

        private BrowsersConfiguration browsersConfig;

        private ObservableCollection<Browser> mBrowsers;

        #endregion

        #region Properties

        public ObservableCollection<Browser> Browsers
        {
            get
            {
                return mBrowsers;
            }
            set
            {
                mBrowsers = value;
                OnPropertyChanged("Browsers");
            }
        }

        public string SelectedBrowserName { get; set; }

        public int SelectedBrowserIndex
        {
            get
            {
                return browsersConfig.SelectedBrowserIndex;
            }
            set
            {
                if (value >= 0)
                {
                    browsersConfig.SelectedBrowserIndex = value;
                    SelectedBrowserName = browsersConfig.BrowsersList[value].BrowserName;
                    OnPropertyChanged("SelectedBrowserIndex");
                    OnPropertyChanged("SelectedBrowserName");
                }
            }
        }

        #endregion

        #region Methods

        public void Initialize()
        {
            browsersConfig = JsonConvert.DeserializeObject<BrowsersConfiguration>(File.ReadAllText(Data.Configuration.GlobalSettings.SDKPath + "Configuration/Browser Integrator/Configuration.json"));

            Browsers = new ObservableCollection<Browser>();
            foreach (Browser b in browsersConfig.BrowsersList)
            {
                Browsers.Add(b);
            }

            SelectedBrowserName = browsersConfig.BrowsersList[browsersConfig.SelectedBrowserIndex].BrowserName;

            SelectedBrowserIndex = browsersConfig.SelectedBrowserIndex;
        }

        public void ResetDefault()
        {
            File.Copy(Data.Configuration.GlobalSettings.SDKPath + "Configuration/Browser Integrator/Configuration.default.json",
                Data.Configuration.GlobalSettings.SDKPath + "Configuration/Browser Integrator/Configuration.json",
                true);
            Initialize();
        }

        public void Save()
        {
            browsersConfig.BrowsersList = Browsers.ToArray<Browser>();
            browsersConfig.SelectedBrowserIndex = SelectedBrowserIndex;

            File.WriteAllText(Data.Configuration.GlobalSettings.SDKPath + "Configuration/Browser Integrator/Configuration.json",
                JsonConvert.SerializeObject(browsersConfig));
        }

        public void AddBrowser(string name, string description, string exePath)
        {
            Browser browser = new Browser();
            browser.BrowserName = name;
            browser.BrowserDescription = description;
            browser.BrowserExecutablePath = exePath;
            Browsers.Add(browser);
            OnPropertyChanged("Browsers");
        }

        public void ModifySelectedBrowser(string name, string description, string exePath)
        {
            Browsers[SelectedBrowserIndex].BrowserName = name;
        }

        public void RemoveSelectedBrowser()
        {
            Browsers.RemoveAt(SelectedBrowserIndex);
            OnPropertyChanged("Browsers");
        }

        #endregion
    }
}
